package id3.UIs;

import id3.controllers.ContinuousToDiscrete;
import id3.controllers.ID3Alg;
import id3.models.RowData;
import id3.models.TreeNode;
import java.util.ArrayList;
import id3.utils.Constants;
import id3.utils.LoadFile;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author NguyenVan
 */
public class TrainingPanel extends JPanel implements ActionListener {

    private final TreePanel _TreePanel = new TreePanel();
    private final JTextField _txtFile;
    private final JFileChooser _file;
    private final JButton _btnChooseFile;
    private final JButton _btnTraining;

    public TrainingPanel() {
        setVisible(true);
        setLayout(new BorderLayout());
        add(_TreePanel, BorderLayout.CENTER);
        _txtFile = new JTextField("", 30);
        _txtFile.setEditable(false);

        JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(_txtFile);
        _file = new JFileChooser(System.getProperty("user.dir"));
        _btnChooseFile = new JButton("Chọn file");
        jPanel.add(_btnChooseFile);
        _btnTraining = new JButton("Training");
        jPanel.add(_btnTraining);
        add(jPanel, BorderLayout.NORTH);
        _btnChooseFile.addActionListener(this);
        _btnTraining.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == _btnChooseFile) {
            int input = _file.showOpenDialog(this);
            if (input == JFileChooser.APPROVE_OPTION) {
                _txtFile.setText(_file.getSelectedFile().getAbsolutePath());
            }
        } else if (e.getSource() == _btnTraining) {
            if (_txtFile.getText().length() == 0) {
                JOptionPane.showMessageDialog(this, "Cần chọn file trước !", "Error :", JOptionPane.ERROR_MESSAGE);
                return;
            }
            try {
                ArrayList<RowData> data;
                data = LoadFile.getData(_txtFile.getText());
                ArrayList<RowData> dataProcessed = ContinuousToDiscrete.updateData(data);
                ArrayList<Integer> list = new ArrayList<>();
                for (int i = 0; i < Constants.NUMBER_ATTRIBUTE - 1; i++) {
                    list.add(i);
                }
                TreeNode tree = ID3Alg.ID3_Alg(dataProcessed, Constants.LABEL, list);
                TreeNode.instance=tree;
                _TreePanel.setjTree1DefaultTreeModel(tree.convertToTree());
                JOptionPane.showMessageDialog(this, "Xong !");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Đã xảy ra lỗi !", "Error :", JOptionPane.ERROR_MESSAGE);
            }

        }
    }

}
